{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用 Ollama 和 Weaviate 构建用于隐私保护的本地 RAG 系统\n",
    "\n",
    "本示例基于 Ollama 博客的一篇文章，标题为 \"[Embedding models](https://ollama.com/blog/embedding-models)\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置 \n",
    "1. 为操作系统下载并安装 Ollama：https://ollama.com/download\n",
    "2. 使用 `pip install ollama` 安装 Python 库，以便从模型生成向量嵌入。(也可使用 REST API 或 JavaScript 库）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install ollama\n",
    "# pip install -U weaviate-client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Pull relevant LLM and [embedding model](https://ollama.com/blog/embedding-models)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ollama pull llama2\n",
    "# ollama pull all-minilm # mxbai-embed-large"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 选择运行: 测试运行 (`ollama run llama2`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The sky appears blue because of a phenomenon called Rayleigh scattering. When sunlight enters Earth's atmosphere, it encounters tiny molecules of gases such as nitrogen and oxygen. These molecules scatter the light in all directions, but they scatter shorter (blue) wavelengths more than longer (red) wavelengths. This is known as Rayleigh scattering.\n",
      "\n",
      "As a result of this scattering, the blue light is dispersed throughout the atmosphere, giving the sky its blue appearance. The blue color is most visible in the morning and evening when the sun is low on the horizon because the light has to travel through more of the atmosphere to reach our eyes, allowing more time for the blue light to be scattered.\n",
      "\n",
      "It's worth noting that the blue color of the sky can vary depending on a number of factors, including the amount of dust and water vapor in the atmosphere, which can absorb or scatter certain wavelengths of light. For example, during sunrise and sunset, when the sun is low on the horizon, the sky can take on hues of red, orange, and pink due to the scattering of light by atmospheric particles.\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "response = ollama.chat(model='llama2', messages=[\n",
    "  {\n",
    "    'role': 'user',\n",
    "    'content': 'Why is the sky blue?',\n",
    "  },\n",
    "])\n",
    "print(response['message']['content'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'embedding': [-0.021702591329813004,\n",
       "  0.15009357035160065,\n",
       "  -0.3400360345840454,\n",
       "  0.09298147261142731,\n",
       "  -0.39127838611602783,\n",
       "  0.06238943338394165,\n",
       "  -0.33130955696105957,\n",
       "  -0.2786891460418701,\n",
       "  0.09664438664913177,\n",
       "  0.13602189719676971,\n",
       "  0.4002634286880493,\n",
       "  -0.41067466139793396,\n",
       "  0.21043244004249573,\n",
       "  0.031082946807146072,\n",
       "  0.06574380397796631,\n",
       "  -0.1371842920780182,\n",
       "  -0.08826376497745514,\n",
       "  0.023083731532096863,\n",
       "  -0.2947360873222351,\n",
       "  0.28975099325180054,\n",
       "  0.07283814996480942,\n",
       "  0.01543290726840496,\n",
       "  0.2452796846628189,\n",
       "  0.14312325417995453,\n",
       "  -0.2776225209236145,\n",
       "  0.5459028482437134,\n",
       "  -0.09798812866210938,\n",
       "  0.02923489920794964,\n",
       "  0.02753671631217003,\n",
       "  0.15834477543830872,\n",
       "  -0.29225167632102966,\n",
       "  0.003948435187339783,\n",
       "  0.014535926282405853,\n",
       "  -0.03330737352371216,\n",
       "  -0.2809745669364929,\n",
       "  0.23154978454113007,\n",
       "  0.23709622025489807,\n",
       "  0.612492024898529,\n",
       "  0.40364035964012146,\n",
       "  0.3277810215950012,\n",
       "  0.09551140666007996,\n",
       "  -0.007477905601263046,\n",
       "  0.33145445585250854,\n",
       "  -0.1987178474664688,\n",
       "  0.23117336630821228,\n",
       "  -0.31999683380126953,\n",
       "  -0.15714102983474731,\n",
       "  0.32886379957199097,\n",
       "  0.1591758131980896,\n",
       "  0.3830070197582245,\n",
       "  -0.10633336752653122,\n",
       "  -0.3779163360595703,\n",
       "  -0.06335574388504028,\n",
       "  0.02888503670692444,\n",
       "  0.22468352317810059,\n",
       "  -0.13662099838256836,\n",
       "  -0.3946607708930969,\n",
       "  -0.05114562064409256,\n",
       "  -0.0750737264752388,\n",
       "  0.014846203848719597,\n",
       "  0.2771098017692566,\n",
       "  0.24762699007987976,\n",
       "  -0.0807596892118454,\n",
       "  0.18020951747894287,\n",
       "  -0.10787424445152283,\n",
       "  -0.11106055974960327,\n",
       "  0.10673438012599945,\n",
       "  0.01676912233233452,\n",
       "  -0.03313891589641571,\n",
       "  -0.4681059718132019,\n",
       "  -0.1669580638408661,\n",
       "  -0.13353213667869568,\n",
       "  -0.05984427034854889,\n",
       "  0.588444709777832,\n",
       "  -0.690241277217865,\n",
       "  0.4033109247684479,\n",
       "  0.34067559242248535,\n",
       "  0.01959913596510887,\n",
       "  -0.22114047408103943,\n",
       "  0.17639502882957458,\n",
       "  -0.11780333518981934,\n",
       "  0.44363126158714294,\n",
       "  -0.17502981424331665,\n",
       "  0.12849470973014832,\n",
       "  -0.05145959556102753,\n",
       "  0.20652972161769867,\n",
       "  0.15372338891029358,\n",
       "  -0.1748417168855667,\n",
       "  -0.022024299949407578,\n",
       "  -0.12791378796100616,\n",
       "  -0.12482046335935593,\n",
       "  -0.37300729751586914,\n",
       "  0.3348013162612915,\n",
       "  0.009924901649355888,\n",
       "  0.08372540026903152,\n",
       "  0.05337611585855484,\n",
       "  0.34475329518318176,\n",
       "  0.23181286454200745,\n",
       "  -0.3164764642715454,\n",
       "  -0.07851649820804596,\n",
       "  -0.080330990254879,\n",
       "  -0.36906296014785767,\n",
       "  0.07940836250782013,\n",
       "  0.17657560110092163,\n",
       "  -0.05228140577673912,\n",
       "  0.15680699050426483,\n",
       "  -0.3909085988998413,\n",
       "  0.016110632568597794,\n",
       "  -0.06887106597423553,\n",
       "  -0.4764823913574219,\n",
       "  -0.5295385122299194,\n",
       "  0.16592156887054443,\n",
       "  -0.39453214406967163,\n",
       "  0.38629207015037537,\n",
       "  -0.04188971966505051,\n",
       "  0.1296466737985611,\n",
       "  -0.0789070799946785,\n",
       "  -0.09044595807790756,\n",
       "  -0.2526743710041046,\n",
       "  -0.32396456599235535,\n",
       "  -0.07270415872335434,\n",
       "  -0.14889097213745117,\n",
       "  0.20535244047641754,\n",
       "  -0.10211379081010818,\n",
       "  0.6262146234512329,\n",
       "  -0.001491781324148178,\n",
       "  -0.28333917260169983,\n",
       "  -2.210018717116563e-32,\n",
       "  0.21621525287628174,\n",
       "  -0.4405490458011627,\n",
       "  0.2245812714099884,\n",
       "  0.11282235383987427,\n",
       "  0.22313231229782104,\n",
       "  -0.18107499182224274,\n",
       "  -0.3172353506088257,\n",
       "  0.32481735944747925,\n",
       "  -0.2742753326892853,\n",
       "  -0.05880996584892273,\n",
       "  -0.4665745794773102,\n",
       "  0.26639872789382935,\n",
       "  0.002769988030195236,\n",
       "  0.06133321300148964,\n",
       "  0.030428092926740646,\n",
       "  0.2154613584280014,\n",
       "  -0.3482128977775574,\n",
       "  -0.2786923050880432,\n",
       "  0.03529033437371254,\n",
       "  -0.6489147543907166,\n",
       "  -0.266102135181427,\n",
       "  0.5444015264511108,\n",
       "  0.19716082513332367,\n",
       "  -0.10562965273857117,\n",
       "  -0.40842315554618835,\n",
       "  0.14724555611610413,\n",
       "  0.4031989574432373,\n",
       "  -0.5433954000473022,\n",
       "  -0.36450469493865967,\n",
       "  0.12598760426044464,\n",
       "  -0.07294467836618423,\n",
       "  0.09234243631362915,\n",
       "  -0.13638709485530853,\n",
       "  -0.08374717831611633,\n",
       "  -0.32725441455841064,\n",
       "  0.11774803698062897,\n",
       "  -0.15653365850448608,\n",
       "  -0.43038618564605713,\n",
       "  -0.2528173327445984,\n",
       "  -0.01656607910990715,\n",
       "  -0.12638282775878906,\n",
       "  -0.03797027841210365,\n",
       "  0.06114402040839195,\n",
       "  0.1220865249633789,\n",
       "  -0.18937177956104279,\n",
       "  -0.15063504874706268,\n",
       "  -0.13687387108802795,\n",
       "  -0.416470468044281,\n",
       "  0.07791389524936676,\n",
       "  0.22808954119682312,\n",
       "  -0.030072001740336418,\n",
       "  0.24327495694160461,\n",
       "  0.020738013088703156,\n",
       "  0.11312635987997055,\n",
       "  -0.20308968424797058,\n",
       "  -0.028681941330432892,\n",
       "  0.19701895117759705,\n",
       "  0.37422239780426025,\n",
       "  -0.313299298286438,\n",
       "  0.32864928245544434,\n",
       "  0.08319874107837677,\n",
       "  -0.18931031227111816,\n",
       "  0.09481986612081528,\n",
       "  -0.1290951371192932,\n",
       "  -0.0058138612657785416,\n",
       "  0.1472136378288269,\n",
       "  -0.06601773947477341,\n",
       "  0.1989586353302002,\n",
       "  0.608880877494812,\n",
       "  0.09035224467515945,\n",
       "  0.22145870327949524,\n",
       "  0.24014881253242493,\n",
       "  -0.006496774964034557,\n",
       "  -0.09509661793708801,\n",
       "  0.29174792766571045,\n",
       "  -0.03501240536570549,\n",
       "  0.09341610968112946,\n",
       "  0.08965088427066803,\n",
       "  0.05770811811089516,\n",
       "  0.009675804525613785,\n",
       "  0.0038124360144138336,\n",
       "  0.21865952014923096,\n",
       "  0.3101324141025543,\n",
       "  0.0012845546007156372,\n",
       "  -0.5051205158233643,\n",
       "  0.12877079844474792,\n",
       "  0.17668074369430542,\n",
       "  0.2363288402557373,\n",
       "  -0.31745266914367676,\n",
       "  -0.3854566216468811,\n",
       "  0.15840032696723938,\n",
       "  0.5517159700393677,\n",
       "  -0.08996646106243134,\n",
       "  -0.4578399658203125,\n",
       "  0.40151336789131165,\n",
       "  1.8944454302264148e-33,\n",
       "  0.09603863954544067,\n",
       "  -0.16221070289611816,\n",
       "  -0.015545235015451908,\n",
       "  -0.20651227235794067,\n",
       "  -0.08696769177913666,\n",
       "  -0.1920921802520752,\n",
       "  0.20809423923492432,\n",
       "  0.023980597034096718,\n",
       "  -0.3416011333465576,\n",
       "  0.13178463280200958,\n",
       "  -0.18798156082630157,\n",
       "  0.07642962038516998,\n",
       "  -0.1488245129585266,\n",
       "  -0.12669305503368378,\n",
       "  0.027890371158719063,\n",
       "  -0.07710729539394379,\n",
       "  -0.03238947317004204,\n",
       "  -0.11722266674041748,\n",
       "  0.022391319274902344,\n",
       "  -0.6184052228927612,\n",
       "  -0.004255745559930801,\n",
       "  0.19981344044208527,\n",
       "  0.14298711717128754,\n",
       "  -0.40078234672546387,\n",
       "  0.15447095036506653,\n",
       "  -0.36764657497406006,\n",
       "  -0.15103334188461304,\n",
       "  0.1299370527267456,\n",
       "  -0.19109246134757996,\n",
       "  -0.037715137004852295,\n",
       "  -0.09020310640335083,\n",
       "  0.16580255329608917,\n",
       "  0.018757697194814682,\n",
       "  -0.21386775374412537,\n",
       "  0.15066564083099365,\n",
       "  -0.032147713005542755,\n",
       "  -0.3509998023509979,\n",
       "  0.014848656952381134,\n",
       "  -0.12364213168621063,\n",
       "  -0.19667983055114746,\n",
       "  0.062315113842487335,\n",
       "  0.14970600605010986,\n",
       "  -0.2723986506462097,\n",
       "  -0.1721404641866684,\n",
       "  0.40257713198661804,\n",
       "  0.3444192409515381,\n",
       "  -0.0477236844599247,\n",
       "  -0.10657680034637451,\n",
       "  0.011397507041692734,\n",
       "  -0.05431567132472992,\n",
       "  0.5239127278327942,\n",
       "  0.0903012752532959,\n",
       "  0.13093441724777222,\n",
       "  0.10880371928215027,\n",
       "  0.6325736045837402,\n",
       "  -0.11031705141067505,\n",
       "  -0.07835827022790909,\n",
       "  -0.12132937461137772,\n",
       "  0.1159333735704422,\n",
       "  -0.005482152104377747,\n",
       "  0.17484456300735474,\n",
       "  -0.10955652594566345,\n",
       "  -0.09429026395082474,\n",
       "  -0.10824026167392731,\n",
       "  0.07637329399585724,\n",
       "  -0.07539671659469604,\n",
       "  -0.13584773242473602,\n",
       "  -0.24532866477966309,\n",
       "  0.4382590353488922,\n",
       "  0.09993459284305573,\n",
       "  0.27735233306884766,\n",
       "  -0.4471100866794586,\n",
       "  -0.18954628705978394,\n",
       "  -0.19322609901428223,\n",
       "  0.2658289670944214,\n",
       "  -0.2755199670791626,\n",
       "  -0.22317704558372498,\n",
       "  -0.2857556939125061,\n",
       "  0.20439419150352478,\n",
       "  -0.036813218146562576,\n",
       "  -0.15310941636562347,\n",
       "  -0.13762041926383972,\n",
       "  0.29591473937034607,\n",
       "  0.16756992042064667,\n",
       "  -0.08321479707956314,\n",
       "  -0.08374857157468796,\n",
       "  0.33681291341781616,\n",
       "  0.3415456712245941,\n",
       "  0.14816950261592865,\n",
       "  0.3281787633895874,\n",
       "  0.013393035158514977,\n",
       "  -0.08601508289575577,\n",
       "  0.15495190024375916,\n",
       "  -0.14077791571617126,\n",
       "  0.11236481368541718,\n",
       "  -1.0034752762067001e-07,\n",
       "  -0.03876318782567978,\n",
       "  0.14353102445602417,\n",
       "  0.05328653007745743,\n",
       "  0.21538013219833374,\n",
       "  0.08567828685045242,\n",
       "  0.46678435802459717,\n",
       "  -0.36198586225509644,\n",
       "  0.3673556447029114,\n",
       "  0.15739580988883972,\n",
       "  0.33496230840682983,\n",
       "  0.3561929762363434,\n",
       "  -0.011168988421559334,\n",
       "  0.11152113229036331,\n",
       "  0.40554776787757874,\n",
       "  -0.032877273857593536,\n",
       "  -0.06129052862524986,\n",
       "  0.11165933310985565,\n",
       "  -0.013202082365751266,\n",
       "  -0.023103684186935425,\n",
       "  0.13975790143013,\n",
       "  -0.46805834770202637,\n",
       "  -0.12229961901903152,\n",
       "  -0.22268787026405334,\n",
       "  0.09355314821004868,\n",
       "  -0.052904173731803894,\n",
       "  -0.32686373591423035,\n",
       "  -0.18949583172798157,\n",
       "  0.07504169642925262,\n",
       "  0.26273706555366516,\n",
       "  -0.039956994354724884,\n",
       "  -0.5213021636009216,\n",
       "  -0.20743031799793243,\n",
       "  -0.038431208580732346,\n",
       "  -0.5030588507652283,\n",
       "  -0.1806439310312271,\n",
       "  0.2684711217880249,\n",
       "  -0.10094627737998962,\n",
       "  -0.21464721858501434,\n",
       "  0.23262199759483337,\n",
       "  0.05883871018886566,\n",
       "  0.020660415291786194,\n",
       "  -0.6513321399688721,\n",
       "  0.14621634781360626,\n",
       "  0.3055979013442993,\n",
       "  0.44207507371902466,\n",
       "  0.0747411921620369,\n",
       "  0.2995058596134186,\n",
       "  -0.015410095453262329,\n",
       "  -0.5710037350654602,\n",
       "  -0.42376139760017395,\n",
       "  0.2118798792362213,\n",
       "  0.01843087375164032,\n",
       "  0.035425569862127304,\n",
       "  -0.17589356005191803,\n",
       "  -0.43282395601272583,\n",
       "  -0.3759150803089142,\n",
       "  -0.308958500623703,\n",
       "  -0.13737677037715912,\n",
       "  0.27137893438339233,\n",
       "  0.011830795556306839,\n",
       "  0.1924123913049698,\n",
       "  0.08476188778877258,\n",
       "  0.33317285776138306,\n",
       "  0.16225981712341309]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.embeddings(model=\"all-minilm\", \n",
    "                  prompt= \"Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 弟 1 步: 嵌入信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = [\n",
    "  \"Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels\",\n",
    "  \"Llamas were first domesticated and used as pack animals 4,000 to 5,000 years ago in the Peruvian highlands\",\n",
    "  \"Llamas can grow as much as 6 feet tall though the average llama between 5 feet 6 inches and 5 feet 9 inches tall\",\n",
    "  \"Llamas weigh between 280 and 450 pounds and can carry 25 to 30 percent of their body weight\",\n",
    "  \"Llamas are vegetarians and have very efficient digestive systems\",\n",
    "  \"Llamas live to be about 20 years old, though some only live for 15 years and others live to be 30 years old\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started /Users/leonie/.cache/weaviate-embedded: process ID 32850\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "{\"action\":\"startup\",\"default_vectorizer_module\":\"none\",\"level\":\"info\",\"msg\":\"the default vectorizer modules is set to \\\"none\\\", as a result all new schema classes without an explicit vectorizer setting, will use this vectorizer\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"startup\",\"auto_schema_enabled\":true,\"level\":\"info\",\"msg\":\"auto schema enabled setting is set to \\\"true\\\"\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"level\":\"info\",\"msg\":\"No resource limits set, weaviate will use all available memory and CPU. To limit resources, set LIMIT_RESOURCES=true\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"level\":\"warning\",\"msg\":\"Multiple vector spaces are present, GraphQL Explore and REST API list objects endpoint module include params has been disabled as a result.\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"grpc_startup\",\"level\":\"info\",\"msg\":\"grpc server listening at [::]:50050\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"restapi_management\",\"level\":\"info\",\"msg\":\"Serving weaviate at http://127.0.0.1:8079\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "import weaviate\n",
    "import weaviate.classes as wvc\n",
    "from weaviate.classes.config import Property, DataType\n",
    "\n",
    "client = weaviate.connect_to_embedded()\n",
    "\n",
    "print(client.is_ready())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "{\"level\":\"info\",\"msg\":\"Created shard docs_SN8loOvzlYv7 in 1.157084ms\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":1000,\"index_id\":\"main\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2024-04-09T13:30:52+02:00\",\"took\":38083}\n"
     ]
    }
   ],
   "source": [
    "collection_name = \"docs\"\n",
    "\n",
    "if client.collections.exists(collection_name):\n",
    "    client.collections.delete(collection_name)\n",
    "\n",
    "collection = client.collections.create(\n",
    "    collection_name,\n",
    "    properties=[\n",
    "        Property(name=\"text\", data_type=DataType.TEXT),\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "{\"level\":\"info\",\"msg\":\"Completed loading shard myexampleindex_XGMjGqT60mbO in 2.642125ms\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"level\":\"info\",\"msg\":\"Completed loading shard llamaindex_dWivqPiChdO8 in 5.01475ms\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":3000,\"index_id\":\"main\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2024-04-09T13:30:52+02:00\",\"took\":225208}\n",
      "{\"level\":\"info\",\"msg\":\"Completed loading shard mycontent_oiMgIfNpvwWZ in 706.541µs\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":3000,\"index_id\":\"main\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2024-04-09T13:30:52+02:00\",\"took\":33000}\n",
      "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":3000,\"index_id\":\"main\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2024-04-09T13:30:52+02:00\",\"took\":8005667}\n",
      "{\"level\":\"info\",\"msg\":\"Completed loading shard llamaindex_filter_fKXpSjFDc0KK in 5.150667ms\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":3000,\"index_id\":\"main\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2024-04-09T13:30:52+02:00\",\"took\":385625}\n",
      "{\"level\":\"info\",\"msg\":\"Completed loading shard myexternalcontext_T58iI8xW5iA8 in 4.168958ms\",\"time\":\"2024-04-09T13:30:52+02:00\"}\n",
      "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":3000,\"index_id\":\"main\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2024-04-09T13:30:52+02:00\",\"took\":5969292}\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "\n",
    "# store each document in a vector embedding database\n",
    "with collection.batch.dynamic() as batch:\n",
    "  for i, d in enumerate(documents):\n",
    "    response = ollama.embeddings(model=\"all-minilm\", prompt=d)\n",
    "    embedding = response[\"embedding\"]\n",
    "    batch.add_object(\n",
    "        properties = {\"text\" : d},\n",
    "        vector = embedding,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "QueryReturn(objects=[Object(uuid=_WeaviateUUIDInt('56f66958-dbee-469a-a170-ebfc1b65172e'), metadata=MetadataReturn(creation_time=None, last_update_time=None, distance=None, certainty=None, score=None, explain_score=None, is_consistent=None, rerank_score=None), properties={'text': 'Llamas weigh between 280 and 450 pounds and can carry 25 to 30 percent of their body weight'}, references=None, vector={'default': [0.20390161871910095, 0.23614734411239624, -0.38450300693511963, 0.41218075156211853, -0.3761504888534546, -0.13563679158687592, 0.05481676757335663, -0.08999389410018921, -0.39202630519866943, 0.5020553469657898, 0.33837682008743286, -0.6673769950866699, 0.1679535210132599, 0.3123660385608673, 0.025210224092006683, 0.210839182138443, 0.3430388867855072, -0.213998943567276, -0.30820319056510925, 0.4516986906528473, 0.21469347178936005, -0.06823334842920303, -0.0848790630698204, 0.07997660338878632, -0.3046180009841919, 0.23734384775161743, -0.40219777822494507, -0.008450683206319809, 0.048596084117889404, -0.041891977190971375, -0.46394863724708557, -0.09091179817914963, 0.06493058055639267, -0.1123972088098526, -0.6799883842468262, 0.03633490949869156, 0.053898219019174576, -0.17885512113571167, 0.25682663917541504, 0.26240891218185425, 0.16762204468250275, 0.5017691254615784, 0.06400685012340546, 0.036683619022369385, 0.018679458647966385, -0.49890652298927307, -0.43865659832954407, 0.3001047968864441, 0.018481425940990448, 0.5438300967216492, -0.03533543273806572, -0.32382023334503174, -0.12369605153799057, 0.2477201521396637, 0.11258567869663239, -0.4861973524093628, -0.011881085112690926, -0.4267210364341736, -0.2308068573474884, -0.04821677505970001, 0.2688342332839966, 0.3935123085975647, 0.009952162392437458, -0.05888410285115242, 0.13321788609027863, -0.2236717790365219, -0.36623674631118774, -0.2272404283285141, -0.25023698806762695, -0.19935305416584015, 0.016127046197652817, -0.11143293231725693, -0.1407550573348999, 0.43768471479415894, -0.34951773285865784, -0.15362265706062317, 0.20974719524383545, -0.08247919380664825, -0.17015963792800903, 0.28167957067489624, -0.3633929193019867, -0.03433013707399368, 0.04478272423148155, 0.1255963146686554, 0.20396490395069122, 0.19866180419921875, -0.12755869328975677, 0.2826012969017029, -0.4045286178588867, 0.013326466083526611, 0.08172477036714554, -0.06114944815635681, 0.06982796639204025, -0.013289744034409523, -0.12762102484703064, 0.14036931097507477, -0.32449039816856384, -0.0021496517583727837, -0.23948809504508972, -0.20359888672828674, 0.10313919186592102, -0.22894126176834106, 0.3786284029483795, 0.17538774013519287, -0.2727423310279846, 0.044700391590595245, 0.2451881468296051, 0.3232765197753906, 0.13323673605918884, 0.11221841722726822, -0.271648108959198, -0.008048838004469872, 0.0050802044570446014, 0.6043242812156677, -0.108094722032547, 0.035162221640348434, -0.13659659028053284, -0.014479465782642365, -0.17689767479896545, 0.0537700280547142, -0.027266543358564377, -0.12676560878753662, 0.2828406095504761, -0.4246187210083008, 0.4269537925720215, 0.004925549030303955, -0.3751378655433655, 5.940063154872878e-34, 0.08846211433410645, -0.3159000873565674, 0.19132748246192932, 0.23626059293746948, 0.08255765587091446, -0.23222613334655762, -0.27749401330947876, 0.18769429624080658, 0.14098681509494781, 0.0027011949568986893, -0.40234604477882385, 0.06884467601776123, 0.14881080389022827, 0.13973790407180786, 0.18265582621097565, -0.12307804822921753, -0.12755241990089417, -0.08002340793609619, 0.24063831567764282, -0.3406171202659607, -0.1948944628238678, -0.030762214213609695, 0.14060834050178528, -0.25106534361839294, 0.02095835469663143, 0.20357762277126312, 0.04291487857699394, -0.4194456934928894, -0.314144492149353, 0.058106619864702225, 0.05749842897057533, -0.19029104709625244, -0.3434984087944031, -0.19879955053329468, -0.27633175253868103, 0.10973039269447327, 0.23189382255077362, 0.0070069339126348495, -0.40744662284851074, 0.09159144759178162, 0.4122217893600464, -0.24415647983551025, 0.3204207122325897, 0.2931935787200928, -0.0482974611222744, 0.1420726478099823, 0.23881395161151886, -0.02134416438639164, -0.18850407004356384, 0.1998024731874466, 0.08290926367044449, 0.26303374767303467, 0.07911455631256104, 0.2175436168909073, -0.265519380569458, 0.21212011575698853, 0.16260451078414917, 0.08960941433906555, -0.19952720403671265, 0.3131560683250427, 0.24752353131771088, 0.13509079813957214, 0.071515291929245, -0.18169471621513367, -0.02280387654900551, -0.16870702803134918, -0.053486358374357224, -0.18984635174274445, -0.016250593587756157, 0.09297376126050949, 0.27861249446868896, -0.08373528718948364, 0.4132506251335144, -0.09442859888076782, -0.17916469275951385, 0.07683785259723663, 0.24124374985694885, 0.09081035107374191, -0.06165599450469017, 0.04379938915371895, 0.15585193037986755, 0.2513812780380249, 0.3975215554237366, -0.014040231704711914, -0.2338443100452423, 0.4192071557044983, -0.02070368081331253, 0.17309647798538208, -0.11200884729623795, -0.16322055459022522, 0.339597225189209, 0.23718827962875366, -0.42408227920532227, -0.506540834903717, -0.02221369370818138, -4.6793704428709297e-33, -0.15784615278244019, 0.2614832818508148, -0.26302552223205566, 0.03163084760308266, -0.050466638058423996, -0.2582996189594269, 0.0077776480466127396, 0.3963807225227356, -0.12348710000514984, -0.046700045466423035, -0.1335192620754242, -0.06792553514242172, 0.004305936396121979, 0.10501295328140259, 0.29669731855392456, 0.10340651869773865, -0.17672473192214966, 0.017007160931825638, 0.011902431026101112, -0.6303901672363281, -0.0016287583857774734, -0.07572347670793533, 0.1679215133190155, 0.32470962405204773, -0.15207155048847198, 0.030307400971651077, -0.22439610958099365, -0.06923454999923706, -0.12282570451498032, -0.3151669204235077, -0.10014878213405609, -0.3752254247665405, 0.011415015906095505, 0.02453746646642685, -0.25218215584754944, -0.24985197186470032, -0.36215731501579285, 0.3467864394187927, 0.16760589182376862, 0.2282046377658844, 0.016164081171154976, 0.02537897415459156, -0.0007882844656705856, 0.15696322917938232, 0.03348667174577713, -0.053343869745731354, 0.3311832547187805, -0.49688422679901123, 0.20671480894088745, -0.16931377351284027, 0.4814547598361969, -0.1281822770833969, -0.040048517286777496, 0.00033586472272872925, 0.25120705366134644, -0.28409337997436523, -0.07571408152580261, 0.026915201917290688, 0.15418048202991486, -0.34225234389305115, -0.12795878946781158, 0.3083609938621521, -0.10312168300151825, 0.07547596842050552, -0.009479351341724396, 0.10225266218185425, 0.1786251962184906, -0.08659740537405014, 0.045950111001729965, 0.16881781816482544, -0.0202042069286108, -0.456371545791626, 0.23991480469703674, 0.021099839359521866, 0.0578102208673954, -0.044537514448165894, 0.12322822213172913, -0.02222495898604393, 0.449921190738678, 2.131052315235138e-05, -0.1383029967546463, -0.24680334329605103, 0.3566422760486603, -0.022289736196398735, 0.03415657579898834, 0.07862881571054459, 0.08198811858892441, 0.012117549777030945, -0.2168695330619812, 0.3764412999153137, -0.2225286364555359, 0.25782471895217896, 0.21793247759342194, -0.35635221004486084, 0.0662403553724289, -9.900999486944784e-08, -0.09110690653324127, 0.13477551937103271, -0.17631939053535461, 0.5110820531845093, 0.04438566789031029, 0.6530160903930664, -0.2534448802471161, -0.09010297060012817, 0.2997146248817444, 0.2953689694404602, 0.23891334235668182, -0.18425226211547852, -0.13547825813293457, 0.34478452801704407, -0.2805585265159607, 0.027631983160972595, 0.024983417242765427, 0.020756777375936508, 0.15004616975784302, -0.2802700102329254, -0.31688958406448364, -0.04339088499546051, -0.04161039739847183, 0.19500690698623657, 0.16106662154197693, -0.10088959336280823, -0.5544471740722656, 0.14310528337955475, 0.18300777673721313, 0.46472615003585815, -0.2980903685092926, -0.03990182653069496, -0.3886431157588959, -0.06276129931211472, 0.2888743281364441, -0.005085445009171963, 0.09811632335186005, -0.11560837924480438, 0.1953316032886505, 0.14693841338157654, 0.15396881103515625, -0.35922881960868835, 0.11881886422634125, 0.16132743656635284, 0.31143662333488464, -0.1294727921485901, -0.40169328451156616, -0.005421461537480354, 0.06798385083675385, -0.40343207120895386, 0.300888329744339, -0.3684270679950714, -0.01297798566520214, 0.12604022026062012, -0.3788487911224365, -0.1156391128897667, -0.01734609156847, -0.26546645164489746, -0.054482344537973404, 0.4778689742088318, 0.2617504298686981, -0.015208926051855087, -0.07224859297275543, 0.21683526039123535]}, collection='Docs')])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.query.fetch_objects(limit=1, include_vector=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第 2 步：检索\n",
    "接下来，在示例提示下添加代码，以检索最相关的文档："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels\n"
     ]
    }
   ],
   "source": [
    "# an example prompt\n",
    "prompt = \"What animals are llamas related to?\"\n",
    "\n",
    "# generate an embedding for the prompt and retrieve the most relevant doc\n",
    "response = ollama.embeddings(\n",
    "  prompt=prompt,\n",
    "  model=\"all-minilm\"\n",
    ")\n",
    "\n",
    "results = collection.query.near_vector(near_vector=response[\"embedding\"],\n",
    "                             limit=1)\n",
    "\n",
    "data = results.objects[0].properties['text']\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第三步：生成\n",
    "最后，使用上一步中的提示和检索到的文件生成答案！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Llamas are members of the camelid family, which means they are closely related to other animals in the same family, including:\n",
      "\n",
      "1. Vicuñas: Vicuñas are small, wild relatives of llamas and alpacas. They are found in the Andean region and are known for their soft, woolly coats.\n",
      "2. Camels: Camels are large, even-toed ungulates that are closely related to llamas and vicuñas. They are found in hot, dry climates around the world and are known for their ability to go without water for long periods of time.\n",
      "3. Guanacos: Guanacos are large, wild animals that are related to llamas and vicuñas. They are found in the Andean region and are known for their distinctive long necks and legs.\n",
      "4. Llama-like creatures: There are also other animals that are sometimes referred to as \"llamas,\" such as the lama-like creatures found in China, which are actually a different species altogether. These creatures are not closely related to vicuñas or camels, but are sometimes referred to as \"llamas\" due to their physical similarities.\n",
      "\n",
      "In summary, llamas are related to vicuñas, camels, guanacos, and other animals that are sometimes referred to as \"llamas.\"\n"
     ]
    }
   ],
   "source": [
    "# generate a response combining the prompt and data we retrieved in step 2\n",
    "output = ollama.generate(\n",
    "  model=\"llama2\",\n",
    "  prompt=f\"Using this data: {data}. Respond to this prompt: {prompt}\"\n",
    ")\n",
    "\n",
    "print(output['response'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
